;
define(function() {

    var navigationMenu;
    if (navigationMenu) {
        navigationMenu = null;
    }

    navigationMenu = {
        init: function() {
            this.bindEvents();
            this.getNavigationMenu("all");
			this.getLyMenu();
        },
        bindEvents: function() {
            //任务链表
            $(".task-i-l-ul li i").on("click", function() {
                $(".task-i-l-ul li").removeClass("i-l-ul-li");
                $(".task-i-l-ul li").find(".task-s-active").removeClass("task-s-a-choose");
                $(this).parent().addClass("i-l-ul-li");
                $(this).parent().find(".task-s-active").addClass("task-s-a-choose");
            });
            //状态筛选
            $(".state>span").click(function() {
                var that = this;
                var stateCls = $(that).attr('class');
                $('#timedTask .task-menu-list li').show();
                $('#timedTask .task-menu-list li').each(function(i, item){
                    var cls = $(this).attr('class');
                    if (!cls) return;
                    stateCls = stateCls.replace("active","").trim();
                    if (cls.indexOf(stateCls) == -1 && stateCls != "all")
                        $(item).hide();
                })
                $(this).addClass("active").siblings().removeClass("active")
            })
            this.xxBindEvents();

            //去蓝底
            $(".nav-middle").on("selectstart", function() {
                return false;
            });
        },
        //任务链表向下展开
        xxBindEvents: function() {
            $(".task-menu").click(function() {
                $(this).parent().siblings().find(".task-menu-content").hide();
                $(this).parent().siblings().find("i").removeClass("active");
                if ($(this).next(".task-menu-content:hidden").length === 0) {
                    $(this).next(".task-menu-content").hide();
                } else {
                    $(this).next(".task-menu-content").show();
                };
                if ($(this).find("i.shrink").hasClass("active")) {
                    $(this).find("i.shrink").removeClass("active");
                } else {
                    $(this).find("i.shrink").addClass("active");
                };
            });
        },
        //获取菜单信息
        getNavigationMenu: function(className) {
            var that = this;
            var userInfo = $("#userInfo").data('info');
            $.ajax({
                type: 'GET',
                url: api.internal.common.getTodayTaskList,
                data: { userId: userInfo.smuser.iid },
                dataType: "json",
                async: false,
                success: function(result) {
                    that.nmInfo = result.data;

                    that.taskSSH = []; //定时任务
                    that.taskUTH = []; //不定时任务

                    //后台传过来的所有任务区分开
                    if (that.nmInfo && that.nmInfo.length > 0) {
                        for (var i = 0; i < that.nmInfo.length; i++) {
                            var obj = that.nmInfo[i];
                            if (obj) {
                                if (obj.taskType === 0) {
                                    //定时任务
                                    that.taskSSH.push(obj);
                                } else if (obj.taskType == 1) {
                                    //不定时任务
                                    that.taskUTH.push(obj);
                                }
                            }
                        }
                    }
                    that.taskSSH.sort(function(i, j) {
                        var prevTime = new Date(i.duty_day + " " + i.fortime);
                        var nextTime = new Date(j.duty_day + " " + j.fortime);
                        return prevTime.getTime() - nextTime.getTime()
                    });
                    /*
                    if (that.taskSSH.length === 0 && userInfo.userAreaCode.length == 6) {
                        that.taskSSH = [{
                            "target": "_frame",
                            "fortime": "15:45",
                            "taskName": "城镇报",
                            "productUrl": "http://10.226.120.45:8091/TJ/ntownFore/init.do",
                            "status": 0
                        }]
                    }
                    */
                    that.getMontage();
                    $("#timedTask ."+className).click();
					if (that.taskSSH.length === 0) {
						$('.task-menu').eq(1).click();
						$("#noTask ul.task-menu-list li").first().click();
					} else {
						$('.task-menu').first().click();
						$("#timedTask ul.task-menu-list li").first().click();
					}
                    
                }
            });
        },
		getLyMenu: function() {
			$.ajax({
                type: 'GET',
                url: "http://10.226.107.35:8089/cpzz/queryTaskInfoNew",
                dataType: "json",
                async: false,
                success: function(lyMenuData) {
                    let data = lyMenuData.data;
					var userInfo = $("#userInfo").data('info');
					for(let j = 0;j<data.length;j++){
						let productUrl = `http://10.226.107.35:8070/#/main?taskId=${data[j].task_id}&productId=${data[j].product_id}&userId=${userInfo.smuser.iid}`
						var $li =
							$('<li  data-href="' + productUrl + '" data-target="_frame">' +
								'<a>' + data[j].task_name + '</a>' +
							  '</li>'
							);
						$li.data('product',  data[j]);
						$("#lyTask .task-menu-list").append($li);
					}
                }
            });
		},
        //拼接菜单
        getMontage: function() {
            //定时任务
            if (this.taskSSH.length > 0) {
                $("#timedTask .task-menu-list").empty();

                for (var i = 0; i < this.taskSSH.length; i++) {
                    var data = this.taskSSH[i];
                    var stateStyle = this.getStateStyle(data.status);
                    var $li =
                        $('<li class="' + stateStyle + '" data-href="' + (data.productUrl || 'undefind') + '" data-target="' + data.target+ '">' +
                            '<i></i>' +
                            '<span>' + data.fortime + '</span>' +
                            '<a>' + data.taskName + '</a>' +
                          '</li>'
                        );
                    $li.data('product',  data);
                    $("#timedTask .task-menu-list").append($li);
                }
            }
			var userInfo = $("#userInfo").data('info');
			 //不定时任务
			this.taskUTH.push({
				"productUrl":`http://10.226.107.35:8070/#/lsyb?taskId=1&userId=${userInfo.smuser.iid}&productId=TJ_LSYB`,
				target:"_frame",
				taskName:"临时预报"
			})
            if (this.taskUTH.length > 0) {
                $("#noTask .task-menu-list").empty();

                for (var i = 0; i < this.taskUTH.length; i++) {
                    var data = this.taskUTH[i];
                    var $li =
                        $('<li data-href="' + data.productUrl + '" data-target="' + data.target+ '">' +
                            '<a>' + data.taskName + '</a>' +
                          '</li>'
                        );
                    $li.data('product',  data);
                    $("#noTask .task-menu-list").append($li);
                }
            }
        },
        //为任务状态添加样式
        getStateStyle: function(state) {
            var result = "task-info-notmake";
            if (state == 1) {
                result = "task-info-complete";
            } else if (state == 2) {
                result = "task-info-overtime";
            } else if (state == 3) {
                result = "task-info-complete";
            }

            return result;
        },
        //主动切换到菜单
        setTask:function(){
            var isAll = $("#timedTask .active").hasClass("all");
            var obj = $(".task-menu-list .active");
            if(isAll){
                obj.removeClass().addClass("task-info-complete active");
            }else{
                obj.hide().removeClass().addClass("task-info-complete active");
            }
        },

    }
    return {
        init: function() {
            CafeJS.menuFunc = navigationMenu;
            navigationMenu.init();
            //主动接收iframe传过来的消息，切换菜单状态
            window.addEventListener('message',function(e){
                if(e.data.type ===  'ybfw'){
                    navigationMenu.setTask();
                }
            })
        }
    }
});